In [ ]:
# Software Engineering for Data Scientists
## *Python Basics*
## DATA 515 A
The course materials are maintained on github. The next lecture will discuss github in detail. Today, you'll get minimal instructions to get access to today's lecture materials.
We will start today with the interactive environment that we will be using often through the course: the Jupyter Notebook.
We will walk through the following steps together:
Download miniconda (be sure to get Version 3.6) and install it on your system (hopefully you have done this before coming to class)
Use the conda command-line tool to update your package listing and install the IPython notebook:
Update conda's listing of packages for your system:
$ conda update conda
Install IPython notebook and all its requirements
$ conda install jupyter notebook
Navigate to the directory containing the course material. For example:
$ cd LectureNotes/02_Procedural_Python
You should see a number of files in the directory, including these:
$ ls
Type jupyter notebook in the terminal to start the notebook
$ jupyter notebook
If everything has worked correctly, it should automatically launch your default browser
Click on Lecture-Python-And-Data.ipynb to open the notebook containing the content for this lecture.
With that, you're set up to use the Jupyter notebook!
a is a list, the a.append(1) adds 1 to the list.
In [1]:
# Integer arithematic
1 + 1
Out[1]:
In [2]:
# Integer division version floating point division
print (6 // 4, 6/ 4)
In [3]:
# Have the full set of "calculator functions" but need the numpy package
import numpy as np
print (6.0 * 3, np.sin(2*np.pi))
In [4]:
# Floats can have a null value called nan, not a number
a = np.nan
3*a
Out[4]:
In [5]:
# Can concatenate, substring, find, count, ...
In [6]:
a = "The lazy"
b = "brown fox"
print ("Concatenation: ", a + b)
print ("First three letters: " + a[0:3])
print ("Index of 'z': " + str(a.find('z')))
In [7]:
a_tuple = (1, 'ab', (1,2))
a_tuple
Out[7]:
In [8]:
a_tuple[2]
Out[8]:
In [9]:
a_list = [1, 'a', [1,2]]
In [10]:
a_list[0]
Out[10]:
In [11]:
a_list.append(2)
a_list
Out[11]:
In [12]:
a_list
Out[12]:
In [13]:
dir(a_list)
Out[13]:
In [14]:
help (a_list)
In [15]:
a_list.count(1)
Out[15]:
In [16]:
dessert_dict = {} # Empty dictionary
dessert_dict['Dave'] = "Cake"
dessert_dict["Joe"] = ["Cake", "Pie"]
print (dessert_dict)
In [17]:
dessert_dict["Dave"]
Out[17]:
In [18]:
# This produces an error
dessert_dict["Bernease"] = {}
dessert_dict
Out[18]:
In [19]:
dessert_dict["Bernease"] = {"Favorite": ["sorbet", "cobbler"], "Dislike": "Brownies"}
In [20]:
# A first name shell game
first_int = 1
second_int = first_int
second_int += 1
second_int
Out[20]:
In [21]:
# What is first_int?
first_int
Out[21]:
In [22]:
# A second name shell game
a_list = ['a', 'aa', 'aaa']
b_list = a_list
b_list.append('bb')
b_list
Out[22]:
In [23]:
# What is a_list?
a_list
Out[23]:
In [24]:
# Create a deep copy
import copy
# A second name shell game
a_list = ['a', 'aa', 'aaa']
b_list = copy.deepcopy(a_list)
b_list.append('bb')
print("b_list = %s" % str(b_list))
print("a_list = %s" % str(a_list))
Key insight: Deep vs. Shallow Copies
Resolving a name in the bash shell is done by searching the directories in the PATH environment variable. The first executable with the name is run.
In [25]:
# Example 1 of name resolution in python
var = 10
def func(val):
var = val + 1
return val
In [26]:
# What is returned?
print("func(2) = %d" % func(2))
# What is var?
print("var = %d" % var)
In [27]:
# Example 2 of name resolution in python
var = 10
def func(val):
return val + var
In [28]:
# What is returned?
print("func(2) = %d" % func(2))
# What is var?
print("var = %d" % var)
Insights on python name resolution
In [29]:
# A list and a dict are objects.
# dict has been implemented so that you see its values when you type
# the instance name.
# This is done with many python objects, like list.
a_dict = {'a': [1, 2], 'b': [3, 4, 5]}
a_dict
Out[29]:
In [30]:
# You access the data and methods (codes) associated with an object by
# using the "." operator. These are referred to collectively
# as attributes. Methods are followed by parentheses;
# values (properties) are not.
a_dict.keys()
Out[30]:
In [31]:
# You can discover the attributes of an object using "dir"
dir(a_dict)
Out[31]: